package com.sqx.modules.health.vo;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PdfTest {

    public static void main(String[] args) throws Exception {

//                Map<String,Object> map = new HashMap<>();
//                List<Object> aaa = new ArrayList();
//        map.put("key","075b6eddd825148a674dfa8a8558ac62");
//        map.put("ds",aaa);
//        System.out.println(map);
//        JSONObject json =new JSONObject(map);
//        System.out.println(json);

//        Date date = new Date();
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(dateFormat.format(date));
//        try {
//            String url = "https://terminal.glxsystem.com/device-api/platform/command/execute";
//            //设置请求头信息
//            HttpHeaders headers = new HttpHeaders();
//            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//            headers.setContentType(type);
//            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//            // 设置请求体
//            Map<String, Object> params = new HashMap<>();
//            params.put("uuid", "494b8e7308ce16cd0856a3a181148b70");
//            params.put("imei", "863995146888083");
//            params.put("cmd", path);
//            if ("setgps".equals(path)) {
//                params.put("cycle", 30);
//            }
//            //设置body部分
//            HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(params), headers);
//            RestTemplate restTemplate = new RestTemplate();
//            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
//            log.info("The remote interface returns data ==> {}", result.getBody());
//            // 将接口返回数据 返回到前端
////            return Result.success(result.getBody());
//        } catch (Exception e) {
//            e.printStackTrace();
////            return Result.error(Constants.CODE_500, "系统错误");
//        }
//        Map<String, Object> params = new HashMap<>();
//        params.put("deviceMac", "861831046819260");
//        params.put("starttime","2022-09-06 10:41:02");
//        params.put("endtime", "2022-09-07 10:41:02");
//        String param = JSON.toJSONString(params);
//        String result = HttpUtils.sendPost("http://taiyibp.tjtzkt.com/getBpDatas.aspx", param);
//        System.out.println(result);

//        JSONObject jsonObject1 = JSONObject.parseObject(result);
//        List<Object> data = (List<Object>) jsonObject1.get("data");
//        System.out.println(data);
//        String createTime = DateUtil.parseStr(new Date(), "yyyy-MM-dd HH:mm:ss");
//        System.out.println(createTime);
//        Calendar beforeTime = Calendar.getInstance();
//        beforeTime.add(Calendar.MINUTE,-5);
//        Date beforeD = beforeTime.getTime();
//        String before5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforeD);
//        System.out.println(before5);
//        fillTemplate();
//    String[] aa =  ApiConsts.DataTypeEnum.BLPRESSURE.getDPre();
//        System.out.println(aa[0]);
//        for(String i:aa){
//            System.out.println(i);
//        }
    }
//
//    public static void main(String[] args) {
//        String str = ApiConsts.DataTypeEnum.BLPRESSURE.getDPre();
//        System.out.println(str);
//        boolean status = str.contains("18F070562");
//        if(!status){
//            System.out.println("包含");
//        }else{
//            System.out.println("不包含");
//        }
//    }

    public static void fillTemplate() {
        // 模板路径
        String templatePath = "D:/aabb.pdf";
        // 生成的新文件路径
        String newPDFPath = "D:/aabb_test.pdf";
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            List<String> list=new ArrayList<String>();
            String[] str = {"南阳市第八人民医院","姓名：车晓晓","性别：男", "年龄：35岁","检查日期：2022-06-04 14:40:30","127","127","127","127","127","115","127","127","127","127","127","127","127","127","127","129","127","127","127","127","127","127","127","127","127","128","127","127","127","127","127","127","127","127","127","127","127","127","127","127","127","127","","","报告日期：2022-06-04"};
            for (int i = 0; i < str.length; i++) {
                list.add(str[i]);
            }
            int i = 0;
            Iterator<String> it = form.getFields().keySet().iterator();
            while (it.hasNext()) {
                String name = it.next().toString();
                form.setField(name, list.get(i));
                i++;
            }
            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
        } catch (IOException e) {
            System.out.println(1);
        } catch (DocumentException e) {
            System.out.println(2);
        }
    }
}
